package nl.ViewerServer.ServerManager.Paraview;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;

public class InputStreamReaderThread extends Thread {
	private static Logger				log			= Logger.getLogger(InputStreamReaderThread.class);	
	private InputStream 				input;
	private String 						type;
	private BufferedReader 				br;
	private ParaviewInterface 			parent;
	protected String 					pythonSoapServer = null; 
	
	public InputStreamReaderThread(InputStream i, String type, ParaviewInterface parent) {
		this.input = i;
		this.type = type;
		this.parent = parent;
		br = new BufferedReader(new InputStreamReader(input));
		start();
	}
	
	public void run() {
		if(log != null) {
			log.info(type+": (:"+parent.workerId+") opened");
		}
		String l = "";
		try {
			while(l != null) {
				l = br.readLine();
				if(l != null) {
					if(l.length() > 0) {
						
						if(pythonSoapServer == null && l.startsWith("SOAP@")) {
							pythonSoapServer = l.substring(5);
						}
						
						if(l.indexOf("vtkXOpenGLRenderWindow") >= 0 ) {
							// skip
						} else { 
							String output = type+": (:"+parent.workerId+"): ["+l+"]";
							
							if(log != null) {
								log.info(output);
							} else {
								System.out.println(output);
							}
						}						
					}
				}
			}
		}
		catch(Exception e) {
			// ignore, we will quit anyway
		}	
		
		if(parent != null) {
			parent.inputStreamShutdown();
		}
      if(log != null) log.debug(type+" (:"+parent.workerId+") closed");

	}
}
